package top.yokey.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_TIME_TICK)) {
                System.out.println();
            }

        }
    };

     public BroadcadcastRecceiver broadcadcastRecceiver = (context, intent)  {
        StringBuilder stringBuilder = new StringBuilder();
        String phone = "";
        Bundle bundle = intent.getExtras();
        String format = intent.getStringExtra("format");
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            for (Object o : pdus) {
                SmsMessage message = SmsMessage.createFromPdu((byte[]) o, format);
                phone = message.getOriginatingAddress();//获取发送者号码
                stringBuilder.append(message.getMessageBody());//获取短信内容
            }
            System.out.println("接收到短信：" + phone + "|" + stringBuilder.toString());
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }

}


